#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../../include/sort.h"
#include "../../include/search.h"


#define STRSIZ 6

typedef enum words {
	hop,
	hat,
	tap,
	bat,
	tip,
	mop,
	mom,
	cat,
	zoo,
	wax,
	top,
	dip,
}words_t;

static int compare_str(const void *str1, const void *str2)
{
	int ret;

	ret = strcmp((const char *)str1, (const char *)str2);

	if (ret > 0)
		return 1;
	if (ret < 0)
		return -1;
	else
		return 0;

}



int main(int argc, char *argv[])
{
	char               *target;

	char               sarray[12][STRSIZ],
	                   tarray[12][STRSIZ];
	words_t              word;
	int                ret;
	
	strcpy(sarray[hop], "hop");
	strcpy(sarray[hat], "hat");
	strcpy(sarray[tap], "tap");
	strcpy(sarray[bat], "bat");
	strcpy(sarray[tip], "tip");
	strcpy(sarray[mop], "mop");
	strcpy(sarray[mom], "mom");
	strcpy(sarray[cat], "cat");
	strcpy(sarray[zoo], "zoo");
	strcpy(sarray[wax], "wax");
	strcpy(sarray[top], "top");
	strcpy(sarray[dip], "dip");
	
	fprintf(stdout, "Performing binary search\n");

	strcpy(tarray[0], "bat");
	strcpy(tarray[1], "top");
	strcpy(tarray[2], "xxx");

	issort(sarray, 12, STRSIZ, compare_str);

	for (word = hop; word <= dip; word++)
   		fprintf(stdout, "sorted[%02d]=%s\n", word, (char *)sarray[word]);

   	target = tarray[0];
	ret = bisearch(sarray, target, 12, STRSIZ, compare_str);

	if (ret < 0)
   		fprintf(stdout, "Could not find %s\n", target);
	else
   		fprintf(stdout, "Found %s at position %d\n", target, ret);


   	return 0;
}
